package com.sunday.pinxinghui.model.products;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sunday.common.adapter.MainFragmentPagerAdapter;
import com.sunday.common.base.BaseActivity;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.model.index.IndexClasssifyFragment;
import com.sunday.pinxinghui.model.index.TodayFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 刘涛 on 2017/8/10.
 */

public class SearchProductListActivity extends BaseActivity {


    @Bind(R.id.edit_query)
    EditText editQuery;
    @Bind(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    private List<String> titles = new ArrayList<>();


    private int catId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_product_list);
        ButterKnife.bind(this);
        catId = getIntent().getIntExtra("catId", 0);
        initView();
        initQuery();
    }


    private void initQuery() {

        editQuery.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String name = editQuery.getText().toString().trim();
                ((ProductClassifyFragment) (fragments[0])).setKeyword(name);
                ((ProductClassifyFragment) (fragments[1])).setKeyword(name);
                ((ProductClassifyFragment) (fragments[2])).setKeyword(name);
                ((ProductClassifyFragment) (fragments[0])).refreshData();
                ((ProductClassifyFragment) (fragments[1])).refreshData();
                ((ProductClassifyFragment) (fragments[2])).refreshData();

            }
        });


    }

    private void initView() {
        titles.add("综合排序");
        titles.add("销量");
        titles.add("价格");
        initFragment();
    }


    private Fragment[] fragments;

    private void initFragment() {
        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(3);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {

            }

            @Override
            public void onTabReselect(int position) {
                if (position == 2) {
                    if (((ProductClassifyFragment) (fragments[2])).getSort() == 3) {
                        ((ProductClassifyFragment) (fragments[2])).setSort(4);
                    } else {
                        ((ProductClassifyFragment) (fragments[2])).setSort(3);
                    }
                    ((ProductClassifyFragment) (fragments[2])).refreshData();
                }
            }
        });

    }

    @OnClick(R.id.btn_back)
    void back() {
        onBackPressed();
    }


    public class PagerAdapter extends FragmentPagerAdapter {


        public PagerAdapter(FragmentManager fm) {
            super(fm);
            fragments = new Fragment[titles.size()];
        }

        @Override
        public Fragment getItem(int position) {
            if (fragments[position] == null) {
                fragments[position] = ProductClassifyFragment.newInstance(position + 1,0);
            }
            return fragments[position];
        }

        @Override
        public int getCount() {
            return titles.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }


}
